package 目录列表器;

import java.io.File;
import java.io.FilenameFilter;
import java.util.Arrays;
import java.util.regex.Pattern;

public class DirList {
	public static void main(String[] args) {
		filterDirFileByClass(args);
	}

	/**
	 * 利用匿名内部类来过滤
	 * anonymous
	 */
	private static void filterDirFileByAnonymousClass(String[] args) {
		File path = new File(".");
		String[] list;
		if (args.length == 0) {
			list = path.list();
		} else {
			list = path.list(new FilenameFilter() {
				private Pattern pattern;

				@Override
				public boolean accept(File dir, String name) {
					// 利用name类型来控制
					return true;
				}
			});
		}
		Arrays.sort(list, String.CASE_INSENSITIVE_ORDER);
		for (String s : list) {
			System.out.println(s);
		}

	}

	/**
	 * 利用实现了 FileNameFilter 接口的类 来过滤
	 *
	 * @param args
	 */
	private static void filterDirFileByClass(String[] args) {
		File path = new File(".");
		String[] list;
		if (args.length == 0) {
			list = path.list();
		} else {
			list = path.list(new DirFilter(args[0]));
		}
		Arrays.sort(list, String.CASE_INSENSITIVE_ORDER);
		for (String s : list) {
			System.out.println(s);
		}
	}
}
